package com.qcxk.yuqian.rtu.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qcxk.yuqian.system.component.constant.SystemConstant;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import com.qcxk.yuqian.rtu.entity.Channel;
import com.qcxk.yuqian.rtu.service.IChannelService;
import com.qcxk.yuqian.system.component.exception.ResultBean;
import com.qcxk.yuqian.system.utils.RedisUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import com.qcxk.yuqian.system.component.behaviour.BehaviourOperator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;


/**
 * @auther lp
 * @create 2021-11-15 03:19:19
 * @describe 前端控制器
 */
@RestController
@RequestMapping("/channel")
public class ChannelController {
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private IChannelService channelService;

    @BehaviourOperator(desc = "查询Channel信息")
    @RequestMapping(value = "/loadChannel")
    public JSONObject loadChannel(Channel channel) {
        JSONObject json = new JSONObject();
        channel = channelService.getById(channel.getId());
        if (channel != null) {
            json.put("code", "0");
            json.put("data", channel);
        } else {
            json.put("code", "1");
            json.put("msg", SystemConstant.SYS_DATABASE_ERROR_CN);
        }
        return json;
    }

    @BehaviourOperator(desc = "查询Channel列表")
    @RequestMapping(value = "/getChannelList")
    public JSONObject getChannelList(Channel channel) {

        QueryWrapper<Channel> channelQueryWrapper = new QueryWrapper<>();
        channelQueryWrapper.orderByAsc("id");
        List<Channel> channels = channelService.list(channelQueryWrapper);

        JSONArray jsonArray = new JSONArray();
        channels.stream().forEach(e -> {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("title", e.getChlName());
            jsonObject.put("key", e.getChlKey());
            jsonObject.put("ellipsis", "true");
            jsonObject.put("tooltip", "true");
            jsonObject.put("width", "100");
            jsonObject.put("align", "center");
            jsonArray.add(jsonObject);
        });

        return ResultBean.success(jsonArray);
    }

    @BehaviourOperator(desc = "查询Channel列表")
    @RequestMapping(value = "/queryChannelList")
    public JSONObject queryChannelList(Page page, Channel channel) {
        JSONObject json = new JSONObject();
        page = channelService.queryChannelPage(page, channel);
        json.put("code", "0");
        json.put("current", page.getCurrent());
        json.put("size", page.getSize());
        json.put("total", page.getTotal());

        List<Channel> list = page.getRecords();

        json.put("dataList", list);
        return json;
    }

    @BehaviourOperator(desc = "单一属性修改Channel")
    @RequestMapping(value = "/updateChannelSingle")
    public JSONObject updateChannelSingle(Integer id, String column, String value) throws NoSuchFieldException, IllegalAccessException {
        JSONObject json = new JSONObject();
        Channel channel = channelService.getById(id);
        Class<? extends Channel> clazz = channel.getClass();// 获取到对象对应的class对象
        Field nameField = clazz.getDeclaredField(column);// 获取私有成员变量:column
        nameField.setAccessible(true);// 设置操作权限为true
        nameField.set(channel, value);
        if (channelService.updateById(channel)) {
            json.put("code", "0");
        } else {
            json.put("code", "1");
            json.put("msg", SystemConstant.SYS_DATABASE_ERROR_CN);
        }
        return json;
    }

    @RequestMapping(value = "/chgoppwd")
    public JSONObject chgoppwd(String oldpwd, String newpwd) {
        Channel item = channelService.getById(999999);
        if (StringUtils.isNotBlank(item.getChlKey())) {
            if (item.getChlKey().equals(oldpwd)) {
                item.setChlKey(newpwd);
                channelService.updateById(item);
                return ResultBean.success("密码修改成功！");
            } else {
                return ResultBean.error("旧密码错误，请重试！");
            }
        } else {
            return ResultBean.error("新密码不能为空！");
        }
    }

    @RequestMapping(value = "/chkoppwd")
    public JSONObject chkoppwd(Channel channel) {
        Channel item = channelService.getById(999999);
        if (StringUtils.isNotBlank(item.getChlKey())) {
            if (item.getChlKey().equals(channel.getChlKey())) {
                return ResultBean.success("验证通过！");
            } else {
                return ResultBean.error("密码错误，请重试！");
            }
        } else {
            return ResultBean.error("密码不能为空！");
        }
    }

    @BehaviourOperator(desc = "整体修改Channel")
    @RequestMapping(value = "/updateChannelMultiple")
    public JSONObject updateChannelMultiple(Channel channel) {
        JSONObject json = new JSONObject();

        if (channelService.updateById(channel)) {
            json.put("code", "0");
        } else {
            json.put("code", "1");
            json.put("msg", SystemConstant.SYS_DATABASE_ERROR_CN);
        }
        return json;
    }

    @BehaviourOperator(desc = "删除Channel")
    @RequestMapping(value = "/deleteChannelById")
    public JSONObject deleteChannelById(Channel channel) {
        JSONObject json = new JSONObject();
        if (channelService.deleteChannelById(channel)) {
            json.put("code", "0");
        } else {
            json.put("code", "1");
            json.put("msg", SystemConstant.SYS_DATABASE_ERROR_CN);
        }
        return json;
    }

    @BehaviourOperator(desc = "新增Channel")
    @RequestMapping(value = "/addChannel")
    public JSONObject addChannel(Channel channel) {
        JSONObject json = new JSONObject();
        if (channelService.save(channel)) {
            json.put("code", "0");
        } else {
            json.put("code", "1");
            json.put("msg", SystemConstant.SYS_DATABASE_ERROR_CN);
        }
        return json;
    }

    @RequestMapping(value = "/editChannelById")
    public JSONObject editChannelById(Channel channel) {
        JSONObject json = new JSONObject();
        if (channelService.updateById(channel)) {
            json.put("code", "0");
        } else {
            json.put("code", "1");
            json.put("msg", SystemConstant.SYS_DATABASE_ERROR_CN);
        }
        return json;
    }

}

